package com.mall.goods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.goods.dao.BrandMapper;
import com.mall.goods.service.BrandService;
import com.mall.pojo.Brand;
import com.mall.pojo.Spec;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author: bamboo_shoot on 20/5/20
 * @description: -
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    @Override
    public Brand findBrandById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }

    @Override
    public void updateBrand(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    @Override
    public void deleteBrandById(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Brand> findBrandListByCondition(Brand brand) {
//        构建查询对象
        Example example = creatExample(brand);
        return brandMapper.selectByExample(example);
    }

    @Override
    public PageInfo<Brand> searchBrandsByPagination(Brand brand, int page, int size) {
//        这里是直接根据你的页数和第几页进行分页处理
        PageHelper.startPage(page, size);
//        这个是根据你输入的关键字去查询对应的内容
        Example example = creatExample(brand);
        return new PageInfo<>(brandMapper.selectByExample(example));
    }

    @Override
    public List<Brand> searchBrandByCategoryName(String category) {
        return brandMapper.searchBrandByCategoryName(category);

    }

    @Override
    public List<Spec> searchSpecificationByCategoryName(String category) {
        return brandMapper.searchSpecificationByCategoryName(category);
    }

    private Example creatExample(Brand brand) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (brand != null) {
            if (StringUtils.isNotEmpty(brand.getName())) {
                criteria.andLike("name", "%" + brand.getName() + "%");
            }
            if (StringUtils.isNotEmpty(brand.getImage())) {
                criteria.andLike("image", "%" + brand.getImage() + "%");
            }
            if (StringUtils.isNotEmpty(brand.getImage())) {
                criteria.andLike("letter", "%" + brand.getLetter() + "%");
            }
            if (StringUtils.isNotEmpty(brand.getImage())) {
                criteria.andLike("seq", "%" + brand.getSeq() + "%");
            }
        }
        return example;
    }
}
